package ch2.link.single;

public class LinkListExam {
    public static void main(String[] args) {
        //测试1：数据为整型
        System.out.println("****************测试1****************");
        Integer[] a = {1, 2, 3, 4, 5};
        LinkListClass<Integer> L1 = new LinkListClass<>();
        
        // 1. 尾插法创建链表
        L1.CreateListR(a);
        System.out.println("L1: " + L1);
        System.out.println("L1长度=" + L1.size());
        
        // 2.在末尾添加元素
        L1.Add(10);
        System.out.println("L1: " + L1);
        
        // 遍历每个元素
        System.out.println("求每个序号的元素值");
        for (int i = 0; i < L1.size(); i++) {
			System.out.println("  序号" + i + "的元素值:" + L1.GetElem(i));
		}
        
        // 重置长度
        System.out.println("重新置长度为5");
        L1.Setsize(5);
        System.out.println("L1: " + L1);

        // 在指定位置插入元素
        int i = 1;
        Integer x = 20;
        System.out.println("在序号" + i + "位置插入" + x);
        L1.Insert(i, x);
        System.out.println("L1: " + L1);

        // 删除指定位置的元素
		i = 3;
        System.out.println("删除序号" + i + "的元素");
        L1.Delete(i);
        System.out.println("L1: " + L1);

        // 替换指定位置的元素的值
		i = 2;
        x = 16;
        System.out.println("设置序号" + i + "的元素值为" + x);
        L1.SetElem(i, x);
        System.out.println("L1: " + L1);
        
        // 获取值为5的元素的序号
        x = 5;
        System.out.println("值为" + x + "的元素序号为" + L1.GetNo(x));

        // 测试2：数据为字符
        System.out.println();
        System.out.println("****************测试2****************");
        Character[] b = {'a', 'b', 'c', 'd', 'e', 'f'};
        LinkListClass<Character> L2 = new LinkListClass<>();

        L2.CreateListR(b);
        System.out.println("L2: " + L2);
        System.out.println("L2长度=" + L2.size());

        L2.Add('x');
        System.out.println("L2: " + L2);

        System.out.println("求每个序号的元素值");
        for (i = 0; i < L2.size(); i++) {
            System.out.println("  序号" + i + "的元素值:" + L2.GetElem(i));
        }

        System.out.println("重新置长度为5");
        L2.Setsize(5);
        System.out.println("L2: " + L2);

        i = 1;
        Character y = 'y';
        System.out.println("在序号" + i + "位置插入" + y);
        L2.Insert(i, y);
        System.out.println("L2: " + L2);

        i = 3;
        System.out.println("删除序号" + i + "的元素");
        L2.Delete(i);
        System.out.println("L2: " + L2);

        i = 2;
        y = 'z';
        System.out.println("设置序号" + i + "的元素值为" + y);
        L2.SetElem(i, y);
        System.out.println("L2: " + L2);

        y = 'd';
        System.out.println("值为" + y + "的元素序号为" + L2.GetNo(y));

        // 测试3：数据为字符串，头插法创建单链表
        System.out.println();
        System.out.println("****************测试3****************");
        String c = "abcdef";
        LinkListClass<Character> L3 = new LinkListClass<>();
        L3.CreateListF(c.chars()            // IntStream，也可使用c.codePoints()
                .mapToObj(ch ->(char)ch)    // Stream<Character
                .toArray(Character[]::new));
        System.out.println("L3: " + L3);
    }
}

